import Vue from 'vue'
import VueRouter from 'vue-router'


// 错误页面
const error = [
  // 404页面
  {
    path: '/404',
    name: 'notfound',
    meta: { title: '404' },
    component: () => import("../views/404.vue")
  },
]

const routes = [
  {
    path: '/',
    redirect: { name: 'home' }
  },
  // 框架页面
  {
    path: '/admin',
    name: 'home',
    redirect: { name: 'index' },
    component: () => import("../views/Home"),
    children: [
      // 系统首页
      {
        path: 'index',
        name: 'index',
        meta: { title: '系统首页' },
        component: () => import("../views/Index")
      },
    ]
  },
  // 错误页面
  ...error,
  // 重定向
  {
    path: '*',
    redirect: { name: 'notfound' }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})


// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 页面SEO设置
  to.meta.title && (document.title = to.meta.title);
  to.meta.keywords && document.querySelector('meta[name="keywords"]').setAttribute('content', to.meta.keywords);
  to.meta.description && document.querySelector('meta[name="description"]').setAttribute('content', to.meta.description);

  next();
})

// 全局后置钩子
router.afterEach((to, from) => {

})


Vue.use(VueRouter)

export default router